package com.open.system.controller;

import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
import com.open.system.domain.LoanBankDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanBankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 银行卡管理
 */
 
@Controller
@RequestMapping("/system/loanBank")
public class LoanBankController extends BaseController {
	@Autowired
	private LoanBankService loanBankService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanBank:loanBank")
	String LoanBank(){
	    return "system/loanBank/loanBank";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanBank:loanBank")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="银行卡认证";
		String detail="查看了银行认证列表";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("shopId",shopId);
		}
		//查询列表数据
        Query query = new Query(params);
        
        List<LoanBankDO> loanBankList=null;
        int total=0;
        if("1".equals(shopId)){
        	loanBankList= loanBankService.list(query);
        	total = loanBankService.count(query);
        }else {
        	loanBankList = loanBankService.list2(query);
    		total = loanBankService.count2(query);
		}
		PageUtils pageUtils = new PageUtils(loanBankList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanBank:add")
	String add(){
	    return "system/loanBank/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanBank:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanBankDO loanBank = loanBankService.get(id);
		model.addAttribute("loanBank", loanBank);
	    return "system/loanBank/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanBank:add")
	public R save( LoanBankDO loanBank){
		loanBank.setCreateTime(new Date());
		if(loanBankService.save(loanBank)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanBank:edit")
	public R update( LoanBankDO loanBank){
		loanBankService.update(loanBank);
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="银行卡认证";
		String detail="修改了【"+loanBank.getRealName()+"】的银行卡信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanBank:remove")
	public R remove( Long id){
		if(loanBankService.remove(id)>0){
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="银行卡认证";
			String detail="删除了【"+id+"】的银行卡信息";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanBank:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanBankService.batchRemove(ids);
		return R.ok();
	}
	
}
